{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 语法格式 \n",
    "if <条件判断1>:\n",
    "    <执行1>\n",
    "elif <条件判断2>:\n",
    "    <执行2>\n",
    "elif <条件判断3>:\n",
    "    <执行3>\n",
    "else:\n",
    "    <执行4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "成人\n"
     ]
    }
   ],
   "source": [
    "age = 20\n",
    "if age >= 18:\n",
    "    print('成人')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "成人\n"
     ]
    }
   ],
   "source": [
    "age = 20\n",
    "if age >= 18:\n",
    "    print('成人')\n",
    "elif 18 > age >= 6:\n",
    "    print('青少年')\n",
    "else:\n",
    "    print('孩子')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "teenager\n"
     ]
    }
   ],
   "source": [
    "#注意顺序\n",
    "age = 20\n",
    "if age >= 6:\n",
    "    print('teenager')\n",
    "elif age >= 18:\n",
    "    print('adult')\n",
    "else:\n",
    "    print('kid')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'女'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#类似冒号表达式, y = x == 1 ? '男' : ’女‘\n",
    "gender = 0\n",
    "y = '男' if gender == 1 else '女'\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#嵌套\n",
    "gender = None\n",
    "y = '男' if gender == 1 else '女' if gender == 0 else '未知性别'\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 4, 6, 8]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x for x in range(1,10) if x % 2 == 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "4\n",
      "6\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "generator  = (x for x in range(1,10) if x % 2 == 0)\n",
    "for i in generator:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'b': 2, 'c': 3}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{a : b for a,b in [('a',1),('b',2),('c',3)] if b >= 2 }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[] False\n",
      "() False\n",
      "set() False\n",
      "{} False\n",
      "0 False\n",
      " False\n",
      "False False\n",
      "None False\n",
      "===========\n",
      "[] False\n"
     ]
    }
   ],
   "source": [
    "x = [list(),tuple(),set(),dict(),0,\"\",False, None]\n",
    "for item in x:\n",
    "    if item:\n",
    "        print(item, 'True')\n",
    "    else:\n",
    "        print(item, \"False\")\n",
    "\n",
    "print(\"===========\")\n",
    "\n",
    "item = x[0]\n",
    "if item != []:\n",
    "    print(item,'True')\n",
    "else:\n",
    "    print(item,\"False\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "小明身高 1.75，体重 80.5kg。请根据 BMI 公式（体重除以身高的平方）帮小明计算他的 BMI 指数，并根据 BMI 指数：\n",
    "\n",
    "低于18.5：过轻\n",
    "18.5-25：正常\n",
    "25-28：过重\n",
    "28-32：肥胖\n",
    "高于32：严重肥胖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的身高 单位：m 1.75\n",
      "请输入你的体重 单位：kg 80.5\n",
      "过重\n"
     ]
    }
   ],
   "source": [
    "height = float(input(\"请输入你的身高 单位：m \"))\n",
    "weight = float(input(\"请输入你的体重 单位：kg \"))\n",
    "bmi = weight/(height**2)\n",
    "if bmi < 18.5:\n",
    "    print(\"过轻\")\n",
    "elif bmi < 25:\n",
    "    print(\"正常\")\n",
    "elif bmi < 28:\n",
    "    print(\"过重\")\n",
    "elif bmi < 32:\n",
    "    print(\"肥胖\")\n",
    "else:\n",
    "    print(\"严重肥胖\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
